package com.soft.javase.day09.book;

import java.util.*;

/**
 * @author yzg
 * @desc TODO
 * @date 2025/4/30
 */
public class LibraryManageSystem {
    private static Map<String, List<Book>> library = new HashMap<>();
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        initLibrary();

        while (true) {
            showLibrary();
            int choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    showAllBook();
                    break;
                case 2:
                    addBook();
                    break;
                case 3:
                    deleteBook();
                    break;
                case 4:
                    modifyBook();
                    break;
                case 5:
                    System.out.println("谢谢你的使用");
                    return;
                default:
                    System.out.println("无效的选择，请重新输入");
            }
        }
    }

    private static void initLibrary() {

        List<Book> classicBooks = new ArrayList<>();
        classicBooks.add(new Book("名著", "西游记", 19.0));
        classicBooks.add(new Book("名著", "水浒传", 29.0));
        library.put("名著", classicBooks);

        List<Book> itBooks = new ArrayList<>();
        itBooks.add(new Book("It书籍", "Java入门到精通", 99.0));
        itBooks.add(new Book("It书籍", "PHP入门到精通", 9.9));
        library.put("It书籍", itBooks);
    }

    private static void showLibrary() {
        System.out.println("---------欢迎来到图书管理系统---------");
        System.out.println("1.查看所有书籍");
        System.out.println("2.添加书");
        System.out.println("3.删除书");
        System.out.println("4.修改书");
        System.out.println("5.退出");
        System.out.print("请输入你的选择：");
    }

    private static void showAllBook() {
        System.out.println("类型\t\t书名\t\t价格");
        for (Map.Entry<String, List<Book>> entry : library.entrySet()) {
            String type = entry.getKey();
            List<Book> books = entry.getValue();

            if (!books.isEmpty()) {
                System.out.println(type);

                for (int i = 0; i < books.size(); i++) {
                    System.out.println("\t\t" + books.get(i).getName() + "  " + books.get(i).getPrice());
                }
            }
        }
        System.out.println();
    }

    private static void addBook() {
        System.out.print("请输入要添加书籍的类型：");
        String type = scanner.next();

        System.out.print("请输入要添加的书名：");
        String name = scanner.next();

        System.out.print("请输入要添加书的价格：");
        double price = scanner.nextDouble();

        Book newBook = new Book(type, name, price);

        if (library.containsKey(type)) {

            library.get(type).add(newBook);
        } else {
            List<Book> books = new ArrayList<>();
            books.add(newBook);
            library.put(type, books);
        }

        System.out.println("添加" + name + "成功\n");
    }

    private static void deleteBook() {
        System.out.print("请输入要删除书籍的类型：");
        String type = scanner.next();

        if (!library.containsKey(type)) {
            System.out.println("该类型不存在\n");
            return;
        }

        System.out.print("请输入要删除的书名：");
        String name = scanner.next();

        List<Book> books = library.get(type);
        boolean found = false;

        Iterator<Book> iterator = books.iterator();
        while (iterator.hasNext()) {
            Book book = iterator.next();
            if (book.getName().equals(name)) {
                iterator.remove();
                found = true;
                break;
            }
        }

        if (found) {
            System.out.println("删除" + name + "书籍成功\n");

            if (books.isEmpty()) {
                library.remove(type);
            }
        } else {
            System.out.println("未找到该书籍\n");
        }
    }

    private static void modifyBook() {
        System.out.print("请输入要修改书籍的类型：");
        String type = scanner.next();

        if (!library.containsKey(type)) {
            System.out.println("该类型不存在\n");
            return;
        }

        System.out.print("请输入要修改的书名：");
        String oldName = scanner.next();

        List<Book> books = library.get(type);
        Book bookToModify = null;

        for (Book book : books) {
            if (book.getName().equals(oldName)) {
                bookToModify = book;
                break;
            }
        }

        if (bookToModify == null) {
            System.out.println("未找到该书籍\n");
            return;
        }

        System.out.print("请输入新的书名：");
        String newName = scanner.next();

        System.out.print("请输入新的价格：");
        double newPrice = scanner.nextDouble();

        bookToModify.setName(newName);
        bookToModify.setPrice(newPrice);

        System.out.println("修改成功\n");
    }
}
